﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLibrary;
using System.IO;
using System.Reflection;
using ControlLibrary;

namespace Finance
{
    public partial class MainView : BaseView
    {
        private List<FinanceViewItem> m_listFinanceView = null;

        public MainView()
        {
            InitializeComponent();
            this.Title = "理财易";
            m_listFinanceView = new List<FinanceViewItem>();
            LoadFinanceView();
            // this.flowLayoutPanel.Controls.Clear();
            GradientButton btnItem = null;
            foreach (FinanceViewItem item in m_listFinanceView)
            {
                btnItem = new GradientButton();
                btnItem.Margin = new Padding(30, 30, 30, 0);
                btnItem.Size = new Size(100, 30);
                btnItem.Text = item.Name;
                btnItem.AssociatedObject = item.FinanceView;
                flowLayoutPanel.Controls.Add(btnItem);
                btnItem.Click += new EventHandler(btnItem_Click);
            }
        }

        void btnItem_Click(object sender, EventArgs e)
        {
            GradientButton btn = sender as GradientButton;
            if (btn != null)
            {
                Type objFinanceView = btn.AssociatedObject as Type;
                if (objFinanceView != null)
                {
                    this.ParentForm.LoadView(objFinanceView);
                }
            }
        }

        /// <summary>
        /// 当加载时触发事件
        /// </summary>
        public override void OnLoad()
        {
            this.ParentForm.Text = this.Title;
        }

        private void LoadFinanceView()
        {
            /* 
             * 1、AccountBook      我的账本
             * 2、NewAccount       新增记录
             * 3、AccountManager   收支项目管理
             * 4、Report           分析报表
             * 5、Search           搜索账目
             * 6、Investment       股票基金(投资)
            */
            DirectoryInfo dir = new DirectoryInfo(Path.Combine(App.CurrentDirectory, "Components"));
            Assembly theAssembly = null;
            foreach (FileInfo file in dir.GetFiles("*.dll"))
            {
                theAssembly = Assembly.LoadFrom(file.FullName);
                if (theAssembly != null)
                {
                    // 枚举可导出类型
                    foreach (System.Type item in theAssembly.GetExportedTypes())
                    {
                        // 查找 FinanceViewAttribute 属性
                        // 存在 FinanceViewAttribute 的一定继承于 BaseView
                        object[] attrs = item.GetCustomAttributes(typeof(FinanceViewAttribute), false);
                        if (attrs.Length > 0)
                        {
                            FinanceViewAttribute attr = attrs[0] as FinanceViewAttribute;
                            if (attr != null)
                            {
                                m_listFinanceView.Add(new FinanceViewItem(attr.Name, item));
                            }
                        }
                    }
                }
            }
        }
    }
}
